/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define _GUI_MAIN_MODULE_
#include "os_obj.h"
#include "systask.h"
#include "pincfg.h"
#include "storage.h"
#include "dataproc.h"
#include "./img/c_file/bmp_c_src.h"
#include "gui_main.h"
#include "string.h"
#include "SPIM.h"
#include "lcd_xh.h"
const uint8_t * lcd_xh_gb2312_cn_get(uint32_t mem_offset, uint16_t font_x);

extern uu8 const SanYouMuXi_50[];
extern uu8 const SanYouMuXi_56[];
extern uu8 const SanYouMuXi_120[];

void lcm_main(void);
void img_check(void);
void spim_sw_spi_init(void);
void page_main_disp_update(void);
void page_uni_disp_update(void);
void page_gb_disp_update(void);
void page_zy_disp_update(void);
void page_pt_line_sim(void);
void page_stri_sim(void);
void lcd_data_write(short y, short xoffset, uint8_t const * pdata);

void lcd_xh_alain_utf8_puts(int16_t x, int16_t y, const char *str, uint16_t mode);
void lcd_hal_init(void);

extern const uint8_t guo_jia_code[];
extern const uint8_t lang_yan[];
extern const uint8_t zhu_zi[];
extern const unsigned char Font_24_48[];
extern const uu8 shan_mu40[];

extern uint8_t LcdRam[];
lcd_xh_def_type lcd_obj;
/*****************************************************************************//*!
*
* @brief   storage task.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void GuiTask(void const *argument)
{
	uint16_t row_start, row_size;
	char dup_req;
	
	osDelay(ms_ticks(20));
lcd_hal_init();
	lcd_obj.gb2312_cn_get = lcd_xh_gb2312_cn_get;
	
	lcd_xh_init(&lcd_obj, LcdRam, 320+32, 240);

	spim_sw_spi_init();

	/*spim io cfg*/
	FLASH_InitExtFlash();

	img_check();

	pulse_init(&gui_st.pulse, sys_st.u_tick);

	lcd_xh_disp_buff_set(LcdRam, 320+32, 240);
	lcd_xh_init(&lcd_obj, LcdRam, 320+32, 240);
	lcd_xh_disp_buff_set(LcdRam,  320+32, 240);

	while (1)
	{
		char cal;
		dup_req = 0;

		//变化圆演示，直径不断的变化，由大到小再由小到大来回缩放
		cal = 24;

		while (cal--)
		{
			lcd_xh_poll(0, 0, 240, lcd_data_write);
			if (lcd_xh_update_cnt_get() == 0)
			{
				dup_req = 1;
				break;
			}
		}
		
		osDelay(2);

		if (dup_req)
		{
			pulse_creat(&gui_st.pulse, sys_st.u_tick);
			
			// 清屏
			gui_st.page_now = gui_st.page_reg ;
			if(gui_st.page_now != gui_st.page_old)
			{
				lcd_xh_ram_fill(0);
			}
			
			// 页面
			switch(gui_st.page_now)
			{
				case 0:
					page_main_disp_update();
					break;
				case 1:
					page_uni_disp_update();
					break;
				case 2:
					page_gb_disp_update();
					break;
				case 3:
					page_zy_disp_update();
					break;
				case 4:
					page_pt_line_sim();
					break;
				case 5 :
					page_stri_sim();
					break;
				default:
					gui_st.page_reg = 0;
					break;
			}
			
			gui_st.page_old = gui_st.page_now;	
		}
	}

}

void page_main_disp_update(void)
{
	static uint32_t xsite = 0, ysite = 0;
	int16_t x_base, y_base;
	
	//- 设置字符间隔
	lcd_xh_space_set(2);

	//- 显示圆环
	lcd_xh_circle(160, 120, 110);
	lcd_xh_circle(160, 120, 111);
	lcd_xh_circle(160, 120, 112);
	//lcd_xh_anti();
	//lcd_xh_square_fill(0, 158, 319, 219);
	//lcd_xh_normal();
	
	//- 字体
	lcd_xh_font_set(16, 16, lcd_xh_font_asc_msk_16);

	//- 显示字符串
	x_base = 160;
	lcd_xh_alain_puts(x_base, 56, "DLCM320X240", TEXT_X_MID);

	//- 反显字符串
	lcd_xh_anti();
	lcd_xh_font_set(48, 48, lcd_xh_font_st48B);
	x_base = 160;
	lcd_xh_alain_puts(x_base, 76, "OTC SYSTEM",TEXT_X_MID | TEXT_SQ(0,2,2) | TEXT_LINE(2,0));
	lcd_xh_normal();

	//- 显示图形
	//lcd_xh_imgage_quick(xsite, ysite, 24, 24, Cdot24lib);
	
	//- 显示汉字
	lcd_xh_font_set(50, 50, lcd_xh_font_asc_msk_16);
	x_base = 160-(4*53-3)/2;
	lcd_xh_put_cns(x_base, 160, 4, SanYouMuXi_50);

	lcd_xh_font_set(56, 56, lcd_xh_font_asc_msk_16);
	lcd_xh_alain_put_cns(160, 158, 4, SanYouMuXi_56, TEXT_X_MID | TEXT_SQ(2,2,0) | TEXT_LINE(2,0));

	//lcd_xh_font_set(120,120,ASC_MSK);
	//lcd_xh_put_cns(5,110, 2, SanYouMuXi_120);

	lcd_xh_half_en();
	lcd_xh_font_set(24, 18, lcd_xh_font_num_st24x18);
	lcd_xh_alain_put_nums(160, 130, "+120.0", TEXT_X_MID);

	x_base = 156;
	lcd_xh_square(x_base, 3, x_base+8, 27);
	lcd_xh_square_fill(x_base + 2, 3 + 2, x_base + 8 - 2, 27 - 2);

	//- 底部菜单
	lcd_xh_square_fill(0, 220, 319, 239);
	lcd_xh_anti();
	lcd_xh_square_fill(0, 219, 319, 219);
	
	lcd_xh_font_set(16, 16, lcd_xh_font_asc_msk_16);
	
	lcd_xh_puts(40+0*80-4*11/2, 222, "MENU");
	lcd_xh_puts(40+1*80-4*11/2, 222, "AUTO");
	lcd_xh_puts(40+2*80-4*11/2, 222, "STOP");
	lcd_xh_puts(40+3*80-4*11/2, 222, "STAR");
	
	lcd_xh_line(1*80, 220, 1*80, 239);
	lcd_xh_line(1*80+1, 220, 1*80+1, 239);
	lcd_xh_line(2*80, 220, 2*80, 239);
	lcd_xh_line(2*80+1, 220, 2*80+1, 239);
	lcd_xh_line(3*80, 220, 3*80, 239);
	lcd_xh_line(3*80+1, 220, 3*80+1, 239);
	
	lcd_xh_normal();
	
	lcd_xh_space_set(5);
	lcd_xh_font_set(24, 24, lcd_xh_font_12_24);
	lcd_xh_alain_utf8_puts(160,16,"<<诸子百家>>",TEXT_X_MID | TEXT_SQ(1,5,3)|TEXT_LINE(1,0)); 
	//lcd_xh_alain_ext_puts(220,2,"烽火狼烟",0);
	
	//-移动坐标
	if (xsite < 295)
	{
		xsite++;
	}

	if (ysite < 200)
	{
		lcd_xh_anti();
		lcd_xh_line(xsite, ysite, xsite + 24, ysite);
		lcd_xh_normal();
		ysite++;
	}
}



void img_check(void)
{
	if (0)
	{
		gui_st.img_lib_ok = 0;
	}
	else
	{
		gui_st.img_lib_ok = 1;
	}
}


